home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-04 | 13.2 KB | 470 lines | [TEXT/MPS ] |
- /*
- * File: ScriptValue.cp
- *
- * Contains: xxx put contents here xxx
- *
- * Written by: Rick Violet
- *
- * Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
- *
- * Change History (most recent first):
- *
- * <4> 7/8/93 RV Fix uninitialized fListHead in VUList class
- * <3+> 7/8/93 RV Fix uninitialized fListHead in VUList class
- * 11/18/92 RV xxx put comment here xxx
- *
- * To Do:
- */
-
- #ifndef __ScriptValue__
- #include "ScriptValue.h"
- #endif
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // ScriptValue
- //—————————————————————————————————————————————————————————————————————————————————————
- // ScriptValue::ScriptValue - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue::ScriptValue()
- {
- fNextValue = nil;
- fValueKind = kVUNullKind;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // ScriptValue::Clone - returns a copy of the ScriptValue object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- ScriptValue::Clone()
- {
- return new ScriptValue();
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNull
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNull::VUNull - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUNull::VUNull()
- {
- fValueKind = kVUNullKind;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNull::Clone - returns a copy of the VUNull object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUNull::Clone()
- {
- return new VUNull();
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUBoolean
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUBoolean::VUBoolean - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUBoolean::VUBoolean( Boolean pFlag )
- {
- fValueKind = kVUBooleanKind;
- fFlag = pFlag;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUBoolean::GetBoolean - returns the value of the Boolean.
- //—————————————————————————————————————————————————————————————————————————————————————
- Boolean
- VUBoolean::GetBoolean()
- {
- return fFlag;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUBoolean::Clone - returns a copy of the VUBoolean object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUBoolean::Clone()
- {
- return new VUBoolean( fFlag );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNumber
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNumber::VUNumber - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUNumber::VUNumber( short pNumber )
- {
- fValueKind = kVUNumberKind;
- fNumber = pNumber;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNumber::GetNumber - returns the value of the number.
- //—————————————————————————————————————————————————————————————————————————————————————
- short
- VUNumber::GetNumber()
- {
- return fNumber;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUNumber::Clone - returns a copy of the VUNumber object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUNumber::Clone()
- {
- return new VUNumber( fNumber );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VULongNumber
- //—————————————————————————————————————————————————————————————————————————————————————
- // VULongNumber::VULongNumber - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VULongNumber::VULongNumber( long pNumber )
- {
- fValueKind = kVULongNumberKind;
- fNumber = pNumber;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VULongNumber::GetNumber - returns the value of the number.
- //—————————————————————————————————————————————————————————————————————————————————————
- long
- VULongNumber::GetNumber()
- {
- return fNumber;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VULongNumber::Clone - returns a copy of the VULongNumber object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VULongNumber::Clone()
- {
- return new VULongNumber( fNumber );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString::VUString - constructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUString::VUString( char* pText )
- {
- fValueKind = kVUStringKind;
- fText = new char[ strlen( pText ) + 1]; //———— allocate a string buffer
- if( fText )
- {
- strcpy( fText, pText );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString::~VUString - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUString::~VUString()
- {
- //———— delete the string buffer
- if( fText )
- {
- delete fText;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString::GetText - returns a pointer the text.
- //—————————————————————————————————————————————————————————————————————————————————————
- char*
- VUString::GetText()
- {
- return fText;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString::GetTextSize - returns the number of characters in the string
- //—————————————————————————————————————————————————————————————————————————————————————
- unsigned short
- VUString::GetTextSize()
- {
- if( fText )
- {
- return strlen( fText );
- }
- else
- {
- return 0;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUString::Clone - returns a copy of the VUString object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUString::Clone()
- {
- return new VUString( fText );
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::VUList - create an empty VUList.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUList::VUList()
- {
- fValueKind = kVUListKind;
- fListHead = nil;
- fCount = 0;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::~VUList - destructor.
- //—————————————————————————————————————————————————————————————————————————————————————
- VUList::~VUList()
- {
- ScriptValue* tValue = fListHead;
- ScriptValue* tNextValue;
-
- while( tValue != nil )
- {
- tNextValue = tValue->GetNextValue();
- delete tValue;
- tValue = tNextValue;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::GetCount - return the number of elements in the list.
- //—————————————————————————————————————————————————————————————————————————————————————
- short
- VUList::GetCount()
- {
- return fCount;
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::GetNthItem - return the number of elements in the list.
- //—————————————————————————————————————————————————————————————————————————————————————
- OSErr
- VUList::GetNthItem( short pIndex, ScriptValuePtr& pItemPtr, ValueKind& pKind )
- {
- ValueKind tKind;
-
- //———— Get a pointer to the item
- pItemPtr = GetNthItem( pIndex );
- if( pItemPtr == nil )
- {
- pKind = kVUNullKind;
- return errAEWrongParameters;
- }
-
- //———— Check the item's kind
- tKind = pItemPtr->GetValueKind();
- if( (tKind == pKind) || (pKind == kVUAnyKind) )
- {
- pKind = tKind;
- return noErr;
- }
- else
- {
- pItemPtr = nil;
- return errAEWrongParameters;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::GetNthItem - return the Nth item in the list as a ScriptValue
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUList::GetNthItem( short pIndex )
- {
- short tCurIndex;
- ScriptValue* tValue;
- ScriptValue* tNextValue;
-
- //———— Make sure we have a list to traverse
- if( fListHead != nil )
- {
- //———— Begin with the first item
- tCurIndex = 1;
- tValue = fListHead;
-
- //———— Traverse the list until the Nth item is found,
- //———— or we reach the end of the list
- while( tCurIndex < pIndex )
- {
- //———— Get the next item in the list
- tNextValue = tValue->GetNextValue();
- if( tNextValue != nil )
- {
- //———— We have a next item, increment count and advance pointer
- tValue = tNextValue;
- tCurIndex++;
- }
- else
- {
- //———— We have reached the end of the list,
- //———— without getting to the Nth item.
- //———— return nil object pointer
- return nil;
- }
- //———— We have reached the indexed item
- //———— return its pointer
- }
- return tValue;
- }
- else
- {
- //———— This list is empty, return nil object pointer
- return nil;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::PutNthItem - insert the ScriptValue in the Nth position of the list,
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- VUList::PutNthItem( ScriptValue* pValue, short pIndex )
- {
- short tCurIndex;
- ScriptValue* tValue;
- ScriptValue* tNextValue;
-
- //———— Is this list empty
- if( fListHead == nil )
- {
- //———— The list is empty, append item as kludgy work around
- fListHead = pValue;
- pValue->SetNextValue( nil );
-
- //———— Keep track of how many items in the list
- fCount = 1;
- return;
- }
- else
- {
- //———— The list is not empty, traverse list to pIndex item
- tValue = fListHead;
- tCurIndex = 1;
- while( tCurIndex < pIndex )
- {
- //———— Get the next item in the list
- tNextValue = tValue->GetNextValue();
- if( tNextValue != nil )
- {
- //———— the next item exists, advance pointer and increment tCurIndex
- tValue = tNextValue;
- tCurIndex++;
- }
- else
- {
- //———— the next item does not exist, append item as kludgy work around
- tValue->SetNextValue( pValue );
- pValue->SetNextValue( nil );
-
- //———— Keep track of how many items in the list
- fCount++;
- return;
- }
- }
-
- //———— We have found the indexed item, insert pValue at this position
- pValue->SetNextValue( tValue->GetNextValue() );
- tValue->SetNextValue( pValue );
-
- //———— Keep track of how many items in the list
- fCount++;
- return;
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::PutNthItem - insert the boolean at the Nth position of the list
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- VUList::PutNthItem( Boolean pFlag, short pIndex )
- {
- VUBoolean* tBooleanVal;
-
- tBooleanVal = new VUBoolean( pFlag );
- if( tBooleanVal )
- {
- this->PutNthItem( tBooleanVal, pIndex );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::PutNthItem - insert the number at the Nth position of the list
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- VUList::PutNthItem( short pNumber, short pIndex )
- {
- VUNumber* tNumVal;
-
- tNumVal = new VUNumber( pNumber );
- if( tNumVal )
- {
- this->PutNthItem( tNumVal, pIndex );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::PutNthItem - insert the long number at the Nth position of the list
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- VUList::PutNthItem( long pNumber, short pIndex )
- {
- VULongNumber* tLongVal;
-
- tLongVal = new VULongNumber( pNumber );
- if( tLongVal )
- {
- this->PutNthItem( tLongVal, pIndex );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::PutNthItem - insert the string at the Nth position of the list
- //—————————————————————————————————————————————————————————————————————————————————————
- void
- VUList::PutNthItem( char* pString, short pIndex )
- {
- VUString* tStrVal;
-
- tStrVal = new VUString( pString );
- if( tStrVal )
- {
- this->PutNthItem( tStrVal, pIndex );
- }
- }
-
- //—————————————————————————————————————————————————————————————————————————————————————
- // VUList::Clone - returns a copy of the VUList object
- //—————————————————————————————————————————————————————————————————————————————————————
- ScriptValue*
- VUList::Clone()
- {
- VUList* tList;
- short i;
- ScriptValue* tVal;
- ScriptValue* tVal2;
-
- tList = new VUList();
- if( tList )
- {
- for( i = 1; i <= fCount; i++ )
- {
- tVal = GetNthItem( i );
- tVal2 = tVal->Clone();
- if( tVal2 )
- {
- tList->PutNthItem( tVal2, i );
- }
- }
- }
- return tList;
- }
-
-